package com.itheima.service.impl;

import com.itheima.mapper.LoginMapper;
import com.itheima.pojo.Emp;
import com.itheima.utils.JwtUtils;
import com.itheima.pojo.LoginInfo;
import com.itheima.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;
    @Override
    public LoginInfo login(Emp emp) {
        LoginInfo loginInfo = loginMapper.login(emp);
        if (loginInfo != null) {
            Map<String,Object> map = new HashMap<>();
            map.put("id",loginInfo.getId());
            map.put("username",loginInfo.getUsername());
           // map.put("token",JwtUtils.generateJwt(map));

            String jwt = JwtUtils.generateJwt(map);
            log.info("生成JWT令牌：{}",jwt);
            loginInfo.setToken(jwt);
            return loginInfo;
        }
        return null;

    }
}
